home *** CD-ROM | disk | FTP | other *** search
- class Vec
- {
- var x;
- var y;
- var px;
- var py;
- var vx;
- var vy;
- var links;
- var distances;
- function Vec(xx, yy)
- {
- this.x = xx;
- this.y = yy;
- this.px = xx;
- this.py = yy;
- this.vx = 0;
- this.vy = 0;
- this.links = [];
- this.distances = [];
- }
- function addLink(other)
- {
- if(Math.random() < 0.5)
- {
- this.links.push(other);
- this.distances.push(this.distance(other));
- }
- else
- {
- this.links.unshift(other);
- this.distances.unshift(this.distance(other));
- }
- }
- function clearLinks()
- {
- this.links = [];
- this.distances = [];
- }
- function update(o)
- {
- this.vx = this.x - this.px;
- this.vy = this.y - this.py;
- this.px = this.x;
- this.py = this.y;
- this.vx *= o;
- this.vy *= o;
- this.x += this.vx;
- this.y += this.vy;
- }
- function distance(other)
- {
- var _loc3_ = this.x - other.x;
- var _loc2_ = this.y - other.y;
- return Math.sqrt(_loc3_ * _loc3_ + _loc2_ * _loc2_);
- }
- function adjustUnit(other, distance)
- {
- var _loc2_ = other.x - this.x;
- var _loc3_ = other.y - this.y;
- var _loc4_ = Math.sqrt(_loc2_ * _loc2_ + _loc3_ * _loc3_);
- var _loc5_ = distance - _loc4_;
- if(_loc4_ == 0)
- {
- return undefined;
- }
- _loc2_ = _loc2_ / _loc4_ * (_loc5_ * 0.5);
- _loc3_ = _loc3_ / _loc4_ * (_loc5_ * 0.5);
- this.x -= _loc2_;
- this.y -= _loc3_;
- other.x += _loc2_;
- other.y += _loc3_;
- }
- function adjustUnitRate(other, distance, alpha)
- {
- var _loc2_ = other.x - this.x;
- var _loc3_ = other.y - this.y;
- var _loc5_ = Math.sqrt(_loc2_ * _loc2_ + _loc3_ * _loc3_);
- var _loc4_ = distance - _loc5_;
- if(_loc5_ == 0)
- {
- return undefined;
- }
- _loc2_ /= _loc5_;
- _loc3_ /= _loc5_;
- this.x -= _loc2_ * (_loc4_ * alpha);
- this.y -= _loc3_ * (_loc4_ * alpha);
- other.x += _loc2_ * (_loc4_ * (1 - alpha));
- other.y += _loc3_ * (_loc4_ * (1 - alpha));
- }
- function adjustLinks()
- {
- var _loc2_ = 0;
- while(_loc2_ < this.links.length)
- {
- this.adjustUnit(this.links[_loc2_],this.distances[_loc2_]);
- _loc2_ = _loc2_ + 1;
- }
- }
- function adjustLinks2(scale)
- {
- var _loc2_ = 0;
- while(_loc2_ < this.links.length)
- {
- this.adjustUnit(this.links[_loc2_],this.distances[_loc2_] * scale);
- _loc2_ = _loc2_ + 1;
- }
- }
- function adjustLinks3(scale, alpha)
- {
- var _loc2_ = 0;
- while(_loc2_ < this.links.length)
- {
- this.adjustUnitRate(this.links[_loc2_],this.distances[_loc2_] * scale,alpha);
- _loc2_ = _loc2_ + 1;
- }
- }
- }
-